package org.monkey.common.constant.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 程序猿字符串工具类
 * @author LiTing
 * @date 2022/9/16 16:13
 **/
public class MonkeyStrUtil {
  private MonkeyStrUtil(){}
  private static final Pattern UP_PATTERN = Pattern.compile("[A-Z]");
  private static final Pattern LOW_PATTERN = Pattern.compile("_[a-z]");
  /***
   * <p>
   * 将驼峰转为下划线
   * </p >
   * @param str 非null字符串
   * @return java.lang.String
   * @author comapss
   * @date 2022/5/9 0:01
   * @since 1.0.0
   **/
  public static String humpToUnderline(String str) {

    Matcher matcher = UP_PATTERN.matcher(str);
    StringBuffer sb = new StringBuffer();
    while(matcher.find()) {
      matcher.appendReplacement(sb,  "_" + matcher.group(0).toLowerCase());
    }
    matcher.appendTail(sb);
    return sb.toString();
  }

  /***
   * <p>
   * 将下划线转为驼峰
   * </p >
   * @param str 非null字符串
   * @return java.lang.String
   * @author comapss
   * @date 2022/5/9 0:01
   * @since 1.0.0
   **/
  public static String underlineToHump(String str) {
    str = str.toLowerCase();
    Matcher matcher = LOW_PATTERN.matcher(str);
    StringBuffer sb = new StringBuffer();
    while(matcher.find()) {
      matcher.appendReplacement(sb,  matcher.group(0).toUpperCase().replace("_",""));
    }
    matcher.appendTail(sb);
    return sb.toString();
  }



  /**
   * 首字母小写
   * @param str 非null字符串
   * @return String
   */
  public static String lowerFirstCase(String str){
    char[] chars = str.toCharArray();
    char a = 'A';
    char z = 'Z';
    if(chars[0]>=a&&chars[0]<=z) {
      chars[0] += 32;
    }
    return String.valueOf(chars);
  }

  /**
   * 首字母大写
   * @param str 非null字符串
   * @return String
   */
  public static String upperFirstCase(String str){
    char[] chars = str.toCharArray();
    char a = 'a';
    char z = 'z';
    if(chars[0]>=a&&chars[0]<=z) {
      chars[0] -= 32;
    }
    return String.valueOf(chars);
  }


}
